<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<p>商家注册</p>
		</header>
		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					商家名称：
				</div>
				<div class="content">
					<input type="text" v-model="business.businessName" placeholder="商家名称">
				</div>
			</li>
			<li>
				<div class="title">
					密码：
				</div>
				<div class="content">
					<input type="password" v-model="business.password" placeholder="密码">
				</div>
			</li>
			<li>
				<div class="title">
					确认密码：
				</div>
				<div class="content">
					<input type="password" v-model="confirmPassword" placeholder="确认密码">
				</div>
			</li>
			<li>
				<div class="title">
					商家地址：
				</div>
				<div class="content">
					<input type="text" v-model="business.address" placeholder="商家地址">
				</div>
			</li>
			<li>
				<div class="title">
					商家类型：
				</div>
				<div class="content">
					<input type="text" v-model="business.ordertypeid" placeholder="商家类型">
				</div>
			</li>
			<li>
				<div class="title">
					商家图片：
				</div>
				<div class="content">
					<input type="text" v-model="business.img" placeholder="商家图片">
				</div>
			</li>
			<li>
				<div class="title">
					起送价格：
				</div>
				<div class="content">
					<input type="text" v-model="business.starprice" placeholder="起送价格">
				</div>
			</li>
			<li>
				<div class="title">
					配送费：
				</div>
				<div class="content">
					<input type="text" v-model="business.deliveryprice" placeholder="配送费">
				</div>
			</li>
		</ul>
		<div class="button-register">
			<button @click="register">注册</button>
		</div>
		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>
<script scoped>
	import Footer from '../components/Footer.vue';

	export default {
		name: 'BusinessRegister',
		data() {
			return {
				business: {
					businessName: '',
					password: '',
					address: '',
					ordertypeid: '',
					img: '',
					starprice: '',
					deliveryprice: ''
				},
				confirmPassword: ''
			}
		},
		methods: {
			checkBusinessPhone() {
				this.$axios.get('Business/BusinessId', {
					params: {
						businessId: this.business.businessId,
					}
				}).then(response => {
					if (response.data == 1) {
						this.business.businessId = '';
						alert('该商家编号已被注册！');
					}
				}).catch(error => {
					console.error(error);
				});
			},
			register() {
				if (this.business.businessName == '') {
					alert('商家名称不能为空！');
					return;
				}
				if (this.business.password == '') {
					alert('密码不能为空！');
					return;
				}
				if (this.business.password != this.confirmPassword) {
					alert('两次输入的密码不一致！');
					return;
				}
				if (this.business.address == '') {
					alert('商家地址不能为空！');
					return;
				}
				if (this.business.explain == '') {
					alert('商家类型不能为空！');
					return;
				}
				if (this.business.img == '') {
					alert('商家图片不能为空！');
					return;
				}
				if (this.business.starprice == '') {
					alert('起送价格不能为空！');
					return;
				}
				if (this.business.deliveryprice == '') {
					alert('配送费不能为空！');
					return;
				}
				// 注册请求
				this.$axios.post('Business/BusinessId', this.$qs.stringify({
						businessName: this.business.businessName,
						businessPassword: this.business.password,
						businessAddress: this.business.address,
						orderTypeId: this.business.ordertypeid,
						businessImg: this.business.img,
						starPrice: this.business.starprice,
						deliveryPrice: this.business.deliveryprice
					}))
					.then(response => {
						if (response.data > 0) {
							this.$axios.get('Business/NewId') .then(response => { // 返回的值为商家ID 
											const businessId = response.data; // 弹出商家账号ID 
											alert(`你的商家账号为: ${businessId}`); }) 
											.catch(error => { console.error("获取商家账号失败: ", error); });
							this.$router.push('/businessLogin'); // 跳转到登录页面
						} else {
							alert('注册失败！');
						}
					}).catch(error => {
						console.error(error);
					});
			}
		},
		components: {
			Footer
		}
	}
</script>
<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/****************** 表单部分 ******************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-register {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-register button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;
		border: none;
		outline: none;
	}
</style>